<?php

class generalFunction {

    //To parse and get directory structurew of the folder
    public function getFileList($dir, $recurse=false, $depth=false , $BASE_URL = "") {
        $retval = array();
        // add trailing slash if missing
        if (substr($dir, -1) != "/") {
            $dir .= "/";
        }

        // open pointer to directory and read list of files
        $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
        while (false !== ($entry = $d->read())) {
            $fileName = $dir . $entry;

            // skip hidden files
            if ($entry[0] == ".") {
                continue;
            }

            if (is_dir($fileName)) {
                $retval[] = array(
                    "name" => $fileName . "/",  #name
                    "url" => $BASE_URL.$fileName . "/",  #name
                    "type" => filetype($fileName), #type
                    "size" => 0, #size
                    "dir" => $dir, #dir
                    "lastmod" => filemtime($fileName) #lastmod
                );
                if ($recurse && is_readable($fileName . "/")) {
                    if ($depth === false) {
                        $retval = array_merge($retval, $this->getFileList($fileName . "/", true, false,$BASE_URL));
                    } elseif ($depth > 0) {
                        $retval = array_merge($retval, $this->getFileList($fileName . "/", true, $depth - 1,$BASE_URL));
                    }
                }
            } elseif (is_readable($fileName)) {
                $retval[] = array(
                    "name" => $fileName,
                    "url" => $BASE_URL.$fileName,
                    "type" => filetype($fileName),
                    "size" => filesize($fileName),
                    "dir" => $dir,
                    "lastmod" => filemtime($fileName)
                );
            }
        }
        $d->close();
        return $retval;
    }

    public function getCurrentScriptName() {
        $scriptArray = explode('/', $_SERVER['SCRIPT_NAME']);
        if (count($scriptArray) > 0) {
            $script_name = end($scriptArray);
            return $script_name;
        }
        return '';
    }

    public function filter($array = '', $filterArray = Array()) {
        /*
          for the filter array the value with  following key will call following functionms
          mysqlRealEscape => mysql_real_escape_string
          htmlEntityDecode => html_entity_decode
          htmlEntity => htmlentities
          trim => TrimElement
          stripslashes => StripSlashElement
         */
        foreach ($filterArray as $filterType) {
            if (is_array($array)) {
                $array = array_map($filterType, $array);
            } else {
                $array = $filterType($array);
            }
        }
        return $array;
    }

    public function ValidUrl($url) {
        $regex = "((https?|ftp)\:\/\/)?"; // SCHEME
        $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
        $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
        $regex .= "(\:[0-9]{2,5})?"; // Port
        $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
        $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
        $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor
        $pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
        if (preg_match("/^$regex$/", $url)) {
            return true;
        }
        return false;
    }

    public function ValidEmailAddress($EmailAddress) {
        if (!preg_match("/^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i", $EmailAddress)) {
            return false;
        }
        return true;
    }

    public function ValidAlphaNumeric($name) {
        if (!preg_match("/^[a-zA-Z0-9 _-]+$/", $name)) {
            return false;
        }
        return true;
    }

    public function isNumeric($n) {
        if (!preg_match("/^[0-9]+$/", $n)) {
            return false;
        }
        return true;
    }

    public function isFloat($string) {
        if (!preg_match("/^[0-9]+(.[0-9]+)?$/", $string)) {
            return false;
        }
        return true;
    }

    public function ValidatePassword($Pwd) {
        if (strlen($Pwd) < 5) {
            return false;
        }
        return true;
    }

    public function IsValidDate($str) {
        $stamp = strtotime($str);

        if (!is_numeric($stamp)) {
            return FALSE;
        }
        $month = date('m', $stamp);
        $day = date('d', $stamp);
        $year = date('Y', $stamp);

        if (checkdate($month, $day, $year)) {
            return TRUE;
        }

        return FALSE;
    }

    public function IsEmpty($value='') {
        if (trim($value) == '') {
            return true;
        }
        return false;
    }

}

function mysqlRealEscape($str = '') {
    if ($str == '') {
        return;
    }
    return $str;
    //return mysql_real_escape_string($str);
}

function htmlEntityDecode($str = '') {
    if ($str == '') {
        return;
    }
    return html_entity_decode($str, ENT_QUOTES);
}

function htmlEntity($str = '') {
    if ($str == '') {
        return;
    }
    return htmlentities($str, ENT_QUOTES);
}

function TrimElement($str = '') {
    if ($str == '') {
        return;
    }
    $str = utf8_encode($str);
    return trim($str);
}

function StripSlashElement($str = '') {
    if ($str == '') {
        return;
    }
    return stripslashes($str);
}

function recursivEncodeArray(&$array = array()) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            recursivEncodeArray($array[$key]);
        } else {
            $array[$key] = utf8_encode($array[$key]);
        }
    }
}

?>
